﻿// Write a program that finds how many times a substring is contained in a given text (perform case insensitive search).

using System;

public class CountSubstring
{
    public static void Main()
    {
        Console.Write("Enter substring to count: ");
        string subString = Console.ReadLine();

        Console.Write("Enter text: ");
        string text = Console.ReadLine();

        Console.WriteLine();
        int count = CountSubStr(subString, text);
        Console.WriteLine("Substring \"{0}\" exist {1} times in the text.", subString, count);
    }

    public static int CountSubStr(string subString, string text)
    {
        int index = -1;
        int count = 0;
        if (subString != string.Empty && text != string.Empty)
        {
            while (true)
            {
                index = text.IndexOf(subString, index + 1);
                if (index == -1)
                {
                    break;
                }

                count++;
            }
        }

        return count;
    }
}